import 'package:cloud_medicine/components/shop_edit&add.dart';
import 'package:cloud_medicine/components/util.dart';
import 'package:cloud_medicine/model/shop_list.dart';
import 'package:cloud_medicine/pages/shopEdit_upimage.dart';
import 'package:flutter/material.dart';

class ShopInfoEdit extends StatefulWidget {
  final num shopID;
  final String address;
  final String name;
  final num phone;
  final num delivery;
  final num inidelivery;
  final String describe;
  ShopInfoEdit(
      {Key key,
      @required this.shopID,
      this.address,
      this.name,
      this.phone,
      this.delivery,
      this.inidelivery,
      this.describe})
      : super(key: key);

  @override
  _ShopInfoEditState createState() => _ShopInfoEditState();
}

class _ShopInfoEditState extends State<ShopInfoEdit> {
  DateTime lastPopTime;

  TextEditingController addressController = new TextEditingController();
  TextEditingController nameController = new TextEditingController();
  TextEditingController phoneController = new TextEditingController();
  TextEditingController deliController = new TextEditingController();
  TextEditingController iniDeliphoneController = new TextEditingController();
  TextEditingController descriController = new TextEditingController();

  bool _loading = false;
  Future<Null> _init() async {
    setState(() {
      _loading = true;
    });
    if (mounted) {
      setState(() {
        _loading = false;
      });
    }
    return;
  }

  @override
  void dispose() {
    super.dispose();
    addressController.dispose();
    nameController.dispose();
    phoneController.dispose();
    deliController.dispose();
    iniDeliphoneController.dispose();
    descriController.dispose();
  }

  @override
  void initState() {
    super.initState();
    _init();
  }

  _childLayout() {
    if (_loading) {
      return Center(
        child: Container(
          child: CircularProgressIndicator(),
        ),
      );
    } else {
      return ListView(
        children: [
          _imgWidget(),
          ShopEditAndAdd(
              shopID: widget.shopID,
              addressController: addressController,
              nameController: nameController,
              phoneController: phoneController,
              oldaddress: widget.address,
              oldname: widget.name,
              oldphone: widget.phone.toString(),
              saveInfoAndCommit: () {
                print(nameController.text); //TODO:
                Navigator.pop(context);
              },
              deliController: deliController,
              iniDeliphoneController: iniDeliphoneController,
              descriController: descriController,
              olddelivery: widget.delivery.toString(),
              oldinidelivery: widget.inidelivery.toString(),
              olddescribe: widget.describe)
        ],
      );
    }
  }

  _imgWidget() {
    List<Widget> _myWidget = [
      GestureDetector(
          child: Container(
            width: 100,
            height: 100,
            decoration: BoxDecoration(
                border: Border.all(width: 1, color: Colors.lightBlueAccent)),
            child: Icon(Icons.add, color: Colors.lightBlueAccent),
          ),
          onTap: () {
            Navigator.of(context).push(MaterialPageRoute(
                builder: (BuildContext context) => ShopEditUpImage(
                      pagekind: 0,
                    )));
          })
    ];
    if (imgUp.isNotEmpty) {
      setState(() {
        _myWidget.insert(
            0,
            Container(
              width: 100,
              height: 100,
              child: Image.network(imgUp[0]),
            ));
        imgUp.clear();
      });
    }
    return Column(
      children: [_myWidget[0]],
    );
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () async {
          if (lastPopTime == null ||
              DateTime.now().difference(lastPopTime) > Duration(seconds: 1)) {
            lastPopTime = DateTime.now();
            openAlertDialog(context, () {
              Navigator.pop(context);
            }, '确认退出编辑，一旦退出，一切将不予保存');
            return new Future.value(false);
          }
          return new Future.value(true);
        },
        child: Scaffold(
          appBar: AppBar(
            title: Text('修改信息'),
          ),
          body: _childLayout(),
        ));
  }
}
